﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.Design;
using Terralever.Facebook.UI.FbmlControls;
using System.ComponentModel;
using System.Web.UI;
using System.ComponentModel.Design;
using System.Globalization;

namespace Terralever.Facebook.UI.Design.FbmlControls
{
    public class MinimumAgeControlDesigner : FbmlControlDesigner
    {
        private class MinimumAgeControlDesignerRegion : TemplatedEditableDesignerRegion
        {
            private object m_obj;
            private PropertyDescriptor m_pd;
            private ITemplate m_template;

            public MinimumAgeControlDesignerRegion(object obj, ITemplate template, PropertyDescriptor descriptor, TemplateDefinition definition) : base(definition)
            {
                m_obj = obj;
                m_template = template;
                m_pd = descriptor;
                base.EnsureSize = true;
            }

            public object Object { get { return m_obj; } }
            public ITemplate Template { get { return m_template; } set { m_template = value; } }
            public PropertyDescriptor PropertyDescriptor { get { return m_pd; } }
        }

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            MinimumAgeControlDesignerRegion rgn = region as MinimumAgeControlDesignerRegion;
            if (rgn != null)
            {
                ITemplate template = rgn.Template;
                if (template != null)
                {
                    IDesignerHost svc = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                    return ControlPersister.PersistTemplate(template, svc);
                }
            }
            return base.GetEditableDesignerRegionContent(region);
        }

        public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
        {
            MinimumAgeControlDesignerRegion rgn = region as MinimumAgeControlDesignerRegion;
            if (rgn != null)
            {
                IDesignerHost svc = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                ITemplate template = ControlParser.ParseTemplate(svc, content);
                rgn.PropertyDescriptor.SetValue(rgn.Object, template);
                rgn.Template = template;
            }
        }

        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);
        }

        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            MinimumAgeControl mac = Component as MinimumAgeControl;
            if (regions == null)
                return base.GetDesignTimeHtml(regions);

            if (mac.DesignerDisplayMode == DesignerDisplayMode.Standard)
            {
                EditableDesignerRegion region = new MinimumAgeControlDesignerRegion(Component, mac.IsOfAgeTemplate, TypeDescriptor.GetProperties(Component)["IsOfAgeTemplate"], IsOfAgeTemplateDefinition);
                region.Description = "The template displayed to users who are as old as the control requires or older.";
                regions.Add(region);
                EditableDesignerRegion region2 = new MinimumAgeControlDesignerRegion(Component, mac.IsNotOfAgeTemplate, TypeDescriptor.GetProperties(Component)["IsNotOfAgeTemplate"], NotOfAgeTemplateDefinition);
                region2.Description = "The template displayed to users who are not yet as old as the control requires.";
                regions.Add(region2);

                return base.CreatePlaceHolderDesignTimeHtml("Click here and use the task menu to edit the conditional requirements templates.");
            }
            else if (mac.DesignerDisplayMode == DesignerDisplayMode.LogicTestPassed)
            {
                EditableDesignerRegion region = new MinimumAgeControlDesignerRegion(Component, mac.IsOfAgeTemplate, TypeDescriptor.GetProperties(Component)["IsOfAgeTemplate"], IsOfAgeTemplateDefinition);
                region.Description = "The template displayed to users who are as old as the control requires or older.";
                regions.Add(region);

                return "<div _designerRegion=\"0\" style=\"display: inline;\"></div>";
            }
            else
            {
                EditableDesignerRegion region2 = new MinimumAgeControlDesignerRegion(Component, mac.IsNotOfAgeTemplate, TypeDescriptor.GetProperties(Component)["IsNotOfAgeTemplate"], NotOfAgeTemplateDefinition);
                region2.Description = "The template displayed to users who are not yet as old as the control requires.";
                regions.Add(region2);

                return "<div _designerRegion=\"0\" style=\"display: inline;\"></div>";
            }
        }

        private TemplateGroupCollection m_templateGroups;

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                if (m_templateGroups == null)
                {
                    m_templateGroups = base.TemplateGroups;
                    TemplateGroup group = new TemplateGroup("<fb:age-plus> Templates");
                    group.AddTemplateDefinition(IsOfAgeTemplateDefinition);
                    group.AddTemplateDefinition(NotOfAgeTemplateDefinition);
                    m_templateGroups.Add(group);
                }

                return m_templateGroups;
            }
        }

        private TemplateDefinition m_tdIsOfAge, m_tdNotOfAge;
        private TemplateDefinition IsOfAgeTemplateDefinition
        {
            get
            {
                if (m_tdIsOfAge == null)
                    m_tdIsOfAge = new TemplateDefinition(this, "Old Enough Template", Component, "IsOfAgeTemplate", false);
                return m_tdIsOfAge;
            }
        }

        private TemplateDefinition NotOfAgeTemplateDefinition
        {
            get
            {
                if (m_tdNotOfAge == null)
                    m_tdNotOfAge = new TemplateDefinition(this, "Too Young Template", Component, "IsNotOfAgeTemplate", false);
                return m_tdNotOfAge;
            }
        }

        public override bool AllowResize
        {
            get
            {
                return InTemplateMode;
            }
        }

        protected override bool UsePreviewControl
        {
            get
            {
                return true;
            }
        }

        private class MinimumAgeControlDesignerActionList : DesignerActionList
        {
            private MinimumAgeControlDesigner m_designer;
            private DesignerActionUIService m_uiSvc;

            public MinimumAgeControlDesignerActionList(IComponent component, MinimumAgeControlDesigner designer)
                : base(component)
            {
                m_designer = designer;
                m_uiSvc = (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
            }

            private PropertyDescriptor GetPropertyByName(string propName)
            {
                PropertyDescriptor prop = TypeDescriptor.GetProperties(Component)[propName];
                return prop;
            }

            private MinimumAgeControl Control
            {
                get { return Component as MinimumAgeControl; }
            }

            public override DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection list = new DesignerActionItemCollection();
                list.Add(new DesignerActionHeaderItem("Design Mode"));

                if (Control.DesignerDisplayMode != DesignerDisplayMode.LogicTestPassed)
                    list.Add(new DesignerActionMethodItem(this, "SetToValidDisplay",
                        "Display Old Enough Template", "Design Mode", true));

                if (Control.DesignerDisplayMode != DesignerDisplayMode.LogicTestFailed)
                    list.Add(new DesignerActionMethodItem(this, "SetToInvalidDisplay",
                        "Display Too Young Template", "Design Mode", true));

                return list;
            }

            public void SetToStandardDisplay()
            {
                Control.DesignerDisplayMode = DesignerDisplayMode.Standard;
                m_designer.UpdateDisplay();
                m_uiSvc.Refresh(Component);
            }

            public void SetToValidDisplay()
            {
                Control.DesignerDisplayMode = DesignerDisplayMode.LogicTestPassed;
                m_designer.UpdateDisplay();
                m_uiSvc.Refresh(Component);
            }

            public void SetToInvalidDisplay()
            {
                Control.DesignerDisplayMode = DesignerDisplayMode.LogicTestFailed;
                m_designer.UpdateDisplay();
                m_uiSvc.Refresh(Component);
            }
        }

        private void UpdateDisplay()
        {
            base.UpdateDesignTimeHtml();
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                var lists = base.ActionLists;
                lists.Add(new MinimumAgeControlDesignerActionList(Component, this));
                return lists;
            }
        }
    }
}
